/**
 * @author 徐楠
 * @date 2022/5/22 9:53
 * @version 1.0
 */

package com.xunan.likou;

import java.util.Arrays;
import java.util.Comparator;

public class LongestSpecialSequenceTwo {
    public static void main(String[] args) {
        String[] strs = {"aba", "cdc", "eae"};
        int luSlength = findLUSlength(strs);
        System.out.println(luSlength);
    }

    /**
     * 判断是否是子序列
     */
    public static boolean isSubsequence(String x, String y) {
        int j = 0;
        for (int i = 0; i < y.length() && j < x.length(); i++)
            if (x.charAt(j) == y.charAt(i))
                j++;
        return j == x.length();
    }

    public static int findLUSlength(String[] strs) {
        //排序，按字符串长度，长的在前
        Arrays.sort(strs, new Comparator<String>() {
            public int compare(String s1, String s2) {
                return s2.length() - s1.length();
            }
        });

        for (int i = 0, j; i < strs.length; i++) {
            boolean flag = true;
            for (j = 0; j < strs.length; j++) {
                if (i == j) {
                    continue;
                }
                if (isSubsequence(strs[i], strs[j])) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return strs[i].length();
            }
        }
        return -1;
    }

}
